//
//  XMNavigationController.m
//  XunMi
//
//  Created by MacBook on 2024/2/18.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMNavigationController.h"

@interface XMNavigationController () <UIGestureRecognizerDelegate>

@end


@implementation XMNavigationController
- (void)viewDidLoad {
    [super viewDidLoad];
    // 添加返回手势
    self.interactivePopGestureRecognizer.delegate = self;
    //禁用侧滑手势
    /*
     id traget = self.interactivePopGestureRecognizer.delegate;
     UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
     [self.view addGestureRecognizer:pan];
     */

    // 设置导航栏背景图
    /*
     [self.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
     */

    self.navigationBar.translucent = NO;
    // 背景颜色
    self.navigationBar.barTintColor = UIColor.whiteColor;
    // 字体颜色
    self.navigationBar.tintColor = [UIColor whiteColor];
    // 去掉细线
    [self.navigationBar setShadowImage:[[UIImage alloc] init]];
    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor], NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0]}];
}
#pragma mark - 设置状态栏颜色
- (UIStatusBarStyle)preferredStatusBarStyle {
    UIViewController *topVC = self.topViewController;
    return [topVC preferredStatusBarStyle];
}

#pragma mark - 重写PUSH方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewControllers.count == 1) {
        // 隐藏tabBar
        viewController.hidesBottomBarWhenPushed = YES;
    } else {
        viewController.hidesBottomBarWhenPushed = NO;
    }
    [super pushViewController:viewController animated:animated];
}

#pragma mark - UIGestureRecognizerDelegate
// 实现代理方法:如果不是第一个控制器就可以滑动
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    // 手势何时有效 : 当导航控制器的子控制器个数 > 1就有效
    return self.childViewControllers.count > 1;
}

// 解决系统返回手势与scrollView滑动手势冲突问题
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    // 首先判断响应gestureRecognizer的view是不是系统UILayoutContainerView
    if ([gestureRecognizer.view isKindOfClass:NSClassFromString(@"UILayoutContainerView")]) {
        // 如果otherGestureRecognizer的响应者是UIScrollView，
        // 再判断otherGestureRecognizer的state是began，
        // 同时判断scrollView的位置是不是正好在最左边
        // 满足条件即可实现返回手势
        if ([otherGestureRecognizer.view isKindOfClass:[UIScrollView class]]) {
            UIScrollView *scrollView = (UIScrollView *)otherGestureRecognizer.view;
            if (otherGestureRecognizer.state == UIGestureRecognizerStateBegan && scrollView.contentOffset.x == 0) {
                return YES;
            }
        }
    }
    return NO;
}
#pragma mark - 重写系统方法,恢复手势响应
- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
    self.interactivePopGestureRecognizer.enabled = YES;
    return [super popToRootViewControllerAnimated:animated];
}

- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    self.interactivePopGestureRecognizer.enabled = YES;
    return [super popToViewController:viewController animated:animated];
}

- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated {
    self.interactivePopGestureRecognizer.enabled = YES;
    return [super popViewControllerAnimated:animated];
}
#pragma mark - 关于旋转的设置
//是否自动旋转
- (BOOL)shouldAutorotate {
    return self.topViewController.shouldAutorotate;
}

//支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];
}

//默认方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

@end
